<?php
/**
 * 后台设置
 * @author liudaiming
 * @version 2014.2.28
 *
 */
class SettingAction extends AdminAction{

    public function _empty($name){
        $this->display('Public:notfound');
    }
    
	/**
	 * 添加用户页面
	 */
	public function useradd(){
		$js_file = Resource::getStatic('validate','js');
		$Model = M('AdminRole');
		$list = $Model->field('role_id,name')->where(array('status'=>'1'))->select();
		if(empty($list)){
			$list = array();
		}
		$this->assign('validate',$js_file);
		$this->assign('role',$list);
		$this->display();
	}

	/**
	 * 添加用户保存
	 */
	public function addSave(){
		$Model = D('AdminUser');
		$password = I('uPsw');
		$repassword = I('uAgainPsw');
		$role = I('role');
		if(empty($role)){
			$this->error("请先设置权限", '/Admin/Setting/useradd');
			return;
		}
		if ($password!=$repassword){
			$this->error("输入的两次密码不一致", '/Admin/Setting/useradd');
			return;
		}
		$data = $Model->create();
		if ($data){
			if ($Model->add()){
				$this->success('数据保存成功', '/Admin/Setting/userlist');
			}else{
				$this->error($Model->getError(), '/Admin/Setting/useradd');
			}
		}else{
			$this->error($Model->getError(), '/Admin/Setting/useradd');
		}
		
	}
	
	/**
	 * 检查email是否存在
	 */
	public function checkemail(){
		$email = I('uEmail');
		$Model = M('AdminUser');
		$where = array('email'=>$email);
		$data = $Model->where($where)->find();
		if (empty($data)) {
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	
	/**
	 * 用户列表
	 */
	public function userlist(){
		$js_file = Resource::getStatic('dialog','js');
		$role = Session('yima_role');//角色
		$perpage = C('PERPAGE');
		$page = I('page',1);
		$Model = M('AdminUser');
		$where = array('');
		$total = $Model->count();
		$list = $Model->field('id,username,realname,email,regtime,sex,phone,state,role')->limit($perpage*($page-1).','.$perpage)->select();
		$this->assign('list',$list);
		$mypage = new Pager($total,$perpage,$page,'/Admin/Setting/userlist');
		$rModel = M('AdminRole');
		$rolelist = $rModel->field('role_id,name')->where(array('status'=>'1'))->select();
		if(empty($rolelist)){
			$rolelist = array();
		}
		$this->assign('rolelist',$rolelist);
		$this->assign('page',$mypage->show());
		$this->assign('dialog',$js_file);
		$this->assign('userrole',$role);
		$this->display();
	}
	
	/**
	 * 批量锁定
	 */
	public function userbatchlock(){
		$userid = I('userid');
		$data = array('state'=>1);
		$Model =M('AdminUser');
		foreach ($userid as $val){
			$res = $Model->where(array('id'=>$val))->save($data);
		}
		$this->success('锁定成功','/Admin/Setting/userlist');
	}
	
	/**
	 * 批量启用
	 */
	public function userbatchunlock(){
		$userid = I('userid');
		$data = array('state'=>0);
		$Model =M('AdminUser');
		foreach ($userid as $val){
			$res = $Model->where(array('id'=>$val))->save($data);
		}
		$this->success('启用成功','/Admin/Setting/userlist');
	}
	
	/**
	 * 批量删除
	 */
	public function userbatchdelete(){
		$userid = I('userid');
		$Model =M('AdminUser');
		foreach ($userid as $val){
			$res = $Model->where(array('id'=>$val))->delete();
		}
		$this->success('删除成功','/Admin/Setting/userlist');
	}
	
	/**
	 * 单个删除
	 */
	public function userdel(){
		$userid = I('userid');
		$Model =M('AdminUser');
		$res = $Model->where(array('id'=>$userid))->delete();
		$this->success('删除成功','/Admin/Setting/userlist');
	}
	
	/**
	 * 用户编辑
	 */
	public function useredit(){
		$userid = I('userid');
		$Model = M('AdminUser');
		$rModel = M('AdminRole');
		$rolelist = $rModel->field('role_id,name')->where(array('status'=>'1'))->select();
		$data = $Model->where(array('id'=>$userid))->field('id,username,realname,email,sex,phone,role')->find();
		if(empty($rolelist)){
			$rolelist = array();
		}
		
		$this->assign('rolelist',$rolelist);
		$this->assign('info',$data);
		$this->display();
	}
	
	/**
	 * 编辑后保存
	 */
	public function editSave(){
		$userid = I('userid');
		$where = array('id'=>$userid);
		$Model = M('AdminUser');
		$username = I('uName');
		$realname = I('rName');
		$phone = I('uPhone');
		$sex = I('sex');
		$role = I('role');
		$data = array('username'=>$username,'realname'=>$realname,'phone'=>$phone,'sex'=>$sex,'role'=>$role);
		$res = $Model->where($where)->save($data);
		if($res===false){
			$this->error($Model->getError(),'/Admin/Setting/userlist');
		}else{
			$this->success('保存成功','/Admin/Setting/userlist');
		}
	}
	
	/**
	 * 赋予管理员权限(超级管理员功能)
	 */
	public function userbatchunadmin(){
		$role = Session('yima_role');
		if ($role!=2){
			$this->error('您没有权限操作此项','/Admin/Setting/userlist');
			return;
		}
		$userid = I('userid');
		$Model = M('AdminUser');
		$data = array('role'=>1);
		foreach ($userid as $val){
			$res = $Model->where(array('id'=>$val))->save($data);
		}
		$this->success('操作成功','/Admin/Setting/userlist');
	}
	
	/**
	 * 取消管理员权限
	 */
	public function userbatchununadmin(){
		$role = Session('yima_role');
		if ($role!=2){
			$this->error('您没有权限操作此项','/Admin/Setting/userlist');
			return;
		}
		$userid = I('userid');
		$Model = M('AdminUser');
		$data = array('role'=>0);
		foreach ($userid as $val){
			$res = $Model->where(array('id'=>$val))->save($data);
		}
		$this->success('操作成功','/Admin/Setting/userlist');
	}
	
    /**
     * 角色管理页面
     */
    public function role(){
    	$js_file = Resource::getStatic('dialog','js');
    	$Model = M('AdminRole');
    	$list = $Model->select();
    	$this->assign('list',$list);
    	$this->assign('dialog',$js_file);
    	$this->display();
    }
    
    /**
     * 角色添加
     */
    public function roleadd(){
    	$this->display();
    }

    /**
     * 角色添加保存
     */
    public function addroleSave(){
    	$name = I('name');
    	$is_show = I('is_show');
    	$sort = I('sort');
    	$data = array(
    			'name'=>$name,
    			'sort'=>$sort,
    			'status'=>$is_show,
    			'add_time'=>time(),
    			'add_ip'=>sprintf("%u", ip2long(getIP()))
    	);
    	$Model = M('AdminRole');
    	$res = $Model->add($data);
    	if($res){
    		$this->success('操作成功','/Admin/Setting/role');
    	}else{
    		$this->error('操作失败'.$Model->getError(),'/Admin/Setting/role');
    	}
    }
    
    /**
     * 权限设置
     */
    public function setpriv(){
    	$css_file = Resource::getStatic(array('treetable','tableform'));
    	$js_file = Resource::getStatic('treetable','js');
    	$role_id = I('get.roleid');
    	
    	if ($role_id <= 0) {
    		$this->error('非法操作');
    	}
    	$menu = M('AdminMenu');
    	$priv = M('AdminPriv');
    	$op = new Role();
    	$tree = new Tree();
    	$tree->icon = array('│ ', '├─ ', '└─ ');
    	$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
    	$result = $menu->where("status='1'")->select();
    	$priv_data = $priv->select(); // 获取权限表数据
    	
    	//因为thinkphp会自动把空数组转换为null
    	if (! is_array($priv_data)) {
    		$priv_data = array();
    	}
    	foreach ($result as $n => $t) {
    		$result[$n]['checked'] = ($op->is_checked($t, $role_id, $priv_data)) ? ' checked' : '';
    		$result[$n]['level'] = $op->get_level($t['id'], $result);
    		$result[$n]['parentid_node'] = ($t['parentid']) ? ' class="child-of-node-' .
    				$t['parentid'] . '"' : '';
    	}
    	$str = "<tr id='node-\$id' \$parentid_node>
		        <td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuid[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</td>
		        </tr>";
    	$tree->init($result);
    	$categorys = $tree->get_tree(0, $str);
    	$this->assign('categorys', $categorys);
    	$this->assign('role_id',$role_id);
    	$this->assign('css_tree',$css_file);
    	$this->assign('js_tree',$js_file);
    	$this->display();
    }

    /**
     * 权限设置保存
     */
    public function dosetpriv(){
    	$menuid = I('post.menuid','','intval');
    	$role_id = I('post.role_id','','intval');
    	if (is_array($menuid) && count($menuid) > 0) {
    		$priv = M("AdminPriv");
    		$menu = M("AdminMenu");
    		$priv->where("role_id=$role_id")->delete();
    		$menuinfo = $menu->select();
    		foreach ($menuinfo as $_v) $menu_info[$_v['id']] = $_v;
    		foreach($menuid as $menuid){
    			$info = array();
    			$op = new Role();
    			$info = $op->get_menuinfo(intval($menuid),$menu_info);
    			$info['role_id'] = $role_id;
    			$priv->data($info)->add();
    		}
    	} else {
    		$priv = M("AdminPriv");
    		$priv->where("role_id=$role_id")->delete();
    	}
    	$this->success('操作成功','/Admin/Setting/role');
    }
    
    /**
     * 角色修改
     */
    public function roleedit(){
    	echo '开发中';
    }
    
    /**
     * 角色删除
     */
    public function roledel(){
    	echo '开发中';
    }
	
	/**
	 * 管理菜单
	 */
	public function setmenu(){
		$js_file = Resource::getStatic(array('ztree','dialog'),'js');
		$css_file = Resource::getStatic('ztree');
		$this->assign('js_tree',$js_file);
		$this->assign('css_tree',$css_file);
		$this->display();
	}
	
	/**
	 * 获取菜单项
	 * @version 2015-01-09
	 */
	public function getmenutree(){
		$res = array();
		$res[] = array('id'=>0,'pId'=>0,'name'=>'菜单及子项管理','open'=>true,'isParent'=>true);
		$Model = M('AdminMenu');
		$list = $Model->where(array('parentid'=>0))->select();
		foreach ($list as $val){
			if ($Model->where(array('parentid'=>$val['id']))->find()){
				$res[] = array('id'=>$val['id'],'pId'=>$val['parentid'],'name'=>$val['name'],'m'=>$val['m'],'c'=>$val['c'],'a'=>$val['a'],'isParent'=>true);
			}else{
				$res[] = array('id'=>$val['id'],'pId'=>$val['parentid'],'name'=>$val['name'],'m'=>$val['m'],'c'=>$val['c'],'a'=>$val['a']);
			}
		}
		echo json_encode($res);
	}
	
	/**
	 * 获取菜单的子项
	 * @version 2015-01-09
	 */
	public function getmenuchildren(){
		$id = I('id');
		$Model = M('AdminMenu');
		$res = array();
		$list = $Model->where(array('parentid'=>$id))->select();
		foreach ($list as $val){
			if ($Model->where(array('parentid'=>$val['id']))->find()){
				$res[] = array('id'=>$val['id'],'pId'=>$val['parentid'],'name'=>$val['name'],'m'=>$val['m'],'c'=>$val['c'],'a'=>$val['a'],'isParent'=>true);
			}else{
				$res[] = array('id'=>$val['id'],'pId'=>$val['parentid'],'name'=>$val['name'],'m'=>$val['m'],'c'=>$val['c'],'a'=>$val['a']);
			}
		}
		echo json_encode($res);
	}
	
	/**
	 * 保存添加项
	 */
	public function addmenusave(){
		$pid = I('post.pid');
		$name = I('post.name');
		$moudle = I('post.moudle');
		$controller = I('post.controller');
		$action = I('post.action');
		$sort = I('post.sort');
		$data = array(
				'name'=>$name,
				'parentid'=>$pid,
				'm'=>$moudle,
				'c'=>$controller,
				'a'=>$action
		);
		$Model = M('AdminMenu');
		$res = $Model->add($data);
		if ($res) {
			echo $res;
		}else {
			echo 0;
		}
	}
	
	/**
	 * 删除菜单项
	 */
	public function delmenu(){
	    $id = I('id');
	    $model = M('AdminMenu');
	    $res = $model->where('id='.$id)->delete();
	    $res1 = $model->where('parentid='.$id)->delete();
	    if ($res && $res1) {
	        echo 1;
	    } else {
	        echo 0;
	    }
	}
}